package com.wn.class30.cotroller.houtai;

import com.wn.class30.entity.AdminUser;
import com.wn.class30.service.AdminUserService;
import com.wn.class30.tools.QiniuUtil;
import com.wn.class30.tools.QrCodeUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.util.UUID;

@Controller
public class TestController {

    @Resource
    private AdminUserService adminUserService;

    @GetMapping(value = "/test")
    public String testC(){

        return "success";
    }

    @RequestMapping("/houtai/logout")
    public String selectAdminUserById()throws Exception{
        System.out.println("进入退出了");
        return "/houtai/page/adminuser/login.html";
    }

    @RequestMapping("getCode")
    @ResponseBody
    public String getCode(HttpServletRequest request)throws Exception{
        //给商家生成付款二维码
        String geturl = QrCodeUtil.geturl(request, 1);
        File file = new File(geturl);
        String filename = file.getName();
        //取出文件名的后缀名
        String suffix = filename.substring(filename.lastIndexOf("."));
        //利用UUID生成一个不重复的文件名，避免文件名重复覆盖掉源文件
        UUID uuid = UUID.randomUUID();
        String newFileName = uuid+suffix;
        BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(file));
        QiniuUtil.upload(newFileName,bufferedInputStream);
        String imgPath = QiniuUtil.fullPath(newFileName);
        return imgPath;
    }
}
